<?php

namespace app\netdisk\model;

use think\Model;

class ShareInfo extends Model
{
    protected $autoWriteTimestamp = true;
    protected $table = 'disk_share_info';

    ######################################################################################################################
    # 获取器、修改器、搜索器
    ######################################################################################################################

    public function searchShareNameAttr($query, $value, $data)
    {
        $query->where('share_name', 'like', '%' . trim($value) . '%');
    }

    public function searchShareCodeAttr($query, $value, $data)
    {
        $query->where('share_code', 'like', trim($value));
    }

    public function searchPublishTimeAttr($query, $value, $data)
    {
        $query->whereBetweenTime('publish_time', $data['start_time'] . '00:00:01', $data['end_time'] . '23:59:59');
    }

    public function searchShareLinkAttr($query, $value, $data)
    {
        $query->where('share_link', 'like', trim($value));
    }

    ######################################################################################################################
    # 
    ######################################################################################################################

    // 获取资源列表
    public static function getAll($where, $select)
    {
        $model = self::where($select)->withoutField('update_time');

        if ($where['start_time'] !== '' && $where['end_time'] !== '')
            $model = $model->whereBetweenTime('create_time', $where['start_time'] . '00:00:01', $where['end_time'] . '23:59:59');

        if ($where['share_name'] != '')
            $model = $model->withSearch(['share_name'], $where);

        if ($where['share_code'] != '')
            $model = $model->withSearch(['share_code'], $where);

        $count = $model->count();
        $list = $model->order('update_time desc')->page($where['page'], $where['limit'])->select();

        return compact('count', 'list');
    }

    // 查询网盘链接是否存在
    public static function check($diskLink)
    {
        $res1 = self::withSearch(['share_code'], ['share_code' => $diskLink])->count();
        $res2 = self::withSearch(['share_link'], ['share_link' => $diskLink])->count();
        if ($res1 || $res2)
            return true;
        return false;
    }

    // 查询网盘密码
    public static function queryShareLink($diskLink)
    {
        $res1 = self::withSearch(['share_code'], ['share_code' => $diskLink])->field('id, share_link, share_code, share_pass')->find();
        if (!is_null($res1))
            return $res1->toArray();

        $res2 = self::withSearch(['share_link'], ['share_link' => $diskLink])->field('id, share_link, share_code, share_pass')->find();
        if (!is_null($res2))
            return $res2->toArray();

        return false;
    }

    // 查询数量加1
    public static function incData($id)
    {
        return self::where('id', $id)->inc('hits')->update();
    }


    //


}
